package demo.vo.http;

import java.net.MalformedURLException;
import java.net.URL;

import com.alibaba.fastjson.JSONObject;

/**
 * @author mandy.hu
 * 
 */
public class RequestVo {

    private String url;
    private String protocol;
    private String host;
    private String path;
    private String fullPath;
    private String query;
    private String requestMethod;
    private String handler;
    private int responseCode;
    private String cookie;
    private boolean retryIng;
    private int maxRetryCount = 3;
    private int alreadyRetriedCount = 0;

    public RequestVo(String url) throws MalformedURLException {
        URL httpurl = new URL(url);
        this.url = url;
        this.protocol = httpurl.getProtocol();
        this.host = httpurl.getHost();
        this.path = httpurl.getPath();
        this.fullPath = httpurl.getFile();
        this.query = httpurl.getQuery();
        if (path != null) {
            String[] pathSeps = path.split("\\.");
            if (pathSeps.length == 2) {
                this.handler = pathSeps[1];
            }
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {

        this.url = url;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {

        this.path = path;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public void setRequestMethod(String requestMethod) {
        this.requestMethod = requestMethod;
    }

    public String getHandler() {
        return handler;
    }

    public void setHandler(String handler) {
        this.handler = handler;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public int getResponseCode() {
        return responseCode;
    }

    public void setResponseCode(int responseCode) {
        this.responseCode = responseCode;
    }

    @Override
    public boolean equals(Object obj) {

        if (obj instanceof RequestVo) {
            RequestVo r = (RequestVo) obj;
            return url.equalsIgnoreCase(r.getUrl());
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.url.hashCode();
    }

    public boolean tryRetry() {
        this.retryIng = true;
        return this.alreadyRetriedCount++ < this.maxRetryCount;
    }

    public int getAlreadyRetriedCount() {
        return alreadyRetriedCount;
    }

    public boolean isRetryIng() {
        return retryIng;
    }

    public String getCookie() {
        return cookie;
    }

    public void setCookie(String cookie) {
        this.cookie = cookie;
    }

    public String getFullPath() {
        return fullPath;
    }

}
